package com.data.center.controller.mongo;
import com.data.center.entity.mongo.MongoStaff;
import com.data.center.service.mongo.MongoStaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
import java.util.List;

@RestController
@RequestMapping("/api/staff")
public class MongoStaffController {


        @Autowired
        private MongoStaffService staffService;

        // 获取所有员工
        @GetMapping
        public List<MongoStaff> getAllStaff() {
            return staffService.getAllStaff();
        }

        // 根据 staffId 获取员工
        @GetMapping("/{staffId}")
        public ResponseEntity<MongoStaff> getStaffById(@PathVariable String staffId) {
            Optional<MongoStaff> staff = staffService.getStaffById(staffId);
            return staff.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
        }

        // 添加新员工
        @PostMapping
        public MongoStaff createStaff(@RequestBody MongoStaff staff) {
            return staffService.saveStaff(staff);
        }

        // 更新员工信息
        @PutMapping("/{staffId}")
        public ResponseEntity<MongoStaff> updateStaff(@PathVariable String staffId, @RequestBody MongoStaff updatedStaff) {
            MongoStaff updated = staffService.updateStaff(staffId, updatedStaff);
            if (updated != null) {
                return ResponseEntity.ok(updated);
            } else {
                return ResponseEntity.notFound().build();
            }
        }

        // 删除员工
        @DeleteMapping("/{staffId}")
        public ResponseEntity<Void> deleteStaff(@PathVariable String staffId) {
            staffService.deleteByStaffId(staffId);
            return ResponseEntity.noContent().build();
        }

}
